import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        //初始化测试
        ArrayList<Student> list = new ArrayList<>();
        Student stu1 = new Student("114514", "曹熊", 19, "绿城诚园");
        Student stu2 = new Student("100112", "学神", 19, "梦泪移晶");
        Student stu3 = new Student("113123", "强哥", 19, "捉月半猫");
        //加入集合
        list.add(stu1);
        list.add(stu2);
        list.add(stu3);

        menu();
        int select = 0;
        //退出标记
        boolean flag = true;
        while (flag) {
            select = sc.nextInt();
            switch (select) {
                //查看学生
                case 1:
                    display(list);
                    break;
                //添加学生;
                case 2:
                    add(list);
                    break;
                //删除学生
                case 3:
                    delete(list);
                    break;
                //修改学生
                case 4:
                    revise(list);
                    break;
                case 5: {
                    System.out.println("已退出程序");
                    flag = false;
                    break;
                }
                default:
                    System.out.println("没有此选项");
            }
        }
    }

    //主菜单
    public static void menu() {
        System.out.println("----------欢迎来到学生管理系统-----------");
        System.out.println("1:查看学生");
        System.out.println("2:添加学生");
        System.out.println("3:删除学生");
        System.out.println("4:修改学生");
        System.out.println("5:退出");
    }

    //查看学生
    public static void display(ArrayList<Student> list) {
        for (int i = 0; i < list.size(); i++) {
            String id = list.get(i).getId();
            String name = list.get(i).getName();
            int age = list.get(i).getAge();
            String address = list.get(i).getAddress();
            System.out.println("学号:" + id + " 姓名:" + name + " 年龄:" + age + " 住址:" + address);
        }
    }

    //添加学生
    public static void add(ArrayList<Student> list) {
        int num = 0;
        System.out.print("请输入想要添加的学生数量：");
        num = sc.nextInt();
        String id;
        for (int i = 0; i < num; i++) {
            while (true) {
                System.out.print("请输入学号：");
                id = sc.next();
                boolean flag = contains(list, id);
                if (flag) {
                    System.out.println("学号已存在，请重新输入");
                } else {
                    break;
                }
            }

            System.out.print("请输入姓名：");
            String name = sc.next();

            System.out.print("请输入年龄：");
            int age = sc.nextInt();

            System.out.print("请输入住址：");
            String address = sc.next();

            Student stu = new Student(id, name, age, address);
            list.add(stu);
        }
        System.out.println("添加成功");
    }

    //检查id是否存在
    public static boolean contains(ArrayList<Student> list, String id) {
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    //删除学生
    public static void delete(ArrayList<Student> list) {
        System.out.println("请输入想要删除学生的学号:");
        String str = sc.next();
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(str)) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            list.remove(index);
            System.out.println("删除成功");
        } else {
            System.out.println("未找到该学生");
        }
    }

    //修改学生
    public static void revise(ArrayList<Student> list) {
        String id;
        while (true) {
            System.out.print("请输入想要修改学生的id:");
            id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("请进行修改");
                break;
            } else {
                System.out.println("输入的学号不存在,请重新输入");
            }
        }
        int index = 0;
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getId().equals(id)) {
                index = i;
            }
        }

        Student stu = list.get(index);

        System.out.println("请输入新的学号:");
        String newId = sc.next();
        stu.setId(newId);

        System.out.println("请输入新的姓名:");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入新的年龄:");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请输入新的住址:");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("修改成功");
    }

}